//
//  SLTabBarController.m
//  百思不得姐
//
//  Created by Anthony on 17/3/23.
//  Copyright © 2017年 SLZeng. All rights reserved.
//

#import "SLTabBarController.h"
#import "SLTabBar.h"
#import "SLNavigationController.h"

@interface SLTabBarController ()
@end

@implementation SLTabBarController

#pragma mark - 接口
SLSingletonM(TabBarController)

+ (instancetype)tabBarControllerWithAddChildVCsBlock:(void(^)(SLTabBarController *tabBarC))addVCBlock {
    SLTabBarController *tabbarVC = [SLTabBarController sharedTabBarController];
    if (addVCBlock) {
        addVCBlock(tabbarVC);
        
    }
    
    return tabbarVC;
}

- (void)addChildVC:(UIViewController *)vc title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController:(BOOL)isRequired {
    if (isRequired) {
        SLNavigationController *navC = [[SLNavigationController alloc] initWithRootViewController:vc];
        
        return [self setupOneChildViewController:navC title:title normalImageName:normalImageName selectedImageName:selectedImageName];
        
    } else {
        [self setupOneChildViewController:vc title:title normalImageName:normalImageName selectedImageName:selectedImageName];
    }
}

#pragma mark - 系统回调
- (void)viewDidLoad {
    [super viewDidLoad];
    /**** 设置所有UITabBarItem的文字属性 ****/
    [self setupItemTitleTextAttributes];
    
    /**** 更换TabBar ****/
    [self setupTabBar];
}


#pragma mark - 设置UI
/**
 *  更换TabBar
 */
- (void)setupTabBar
{
    [self setValue:[[SLTabBar alloc] init] forKeyPath:@"tabBar"];
}
    
/**
 初始化一个子控制器

 @param vc                  子控制器
 @param title               标题
 @param normalImageName     一般图片名称
 @param selectedImageName   选中图片名称
 */
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName
{
    
    vc.view.backgroundColor = [UIColor colorWithRed:(arc4random_uniform(255))/255.0 green:(arc4random_uniform(255))/255.0 blue:(arc4random_uniform(255))/255.0 alpha:1];
    vc.tabBarItem.title = title;
    if (normalImageName.length) { // 图片名有具体值
        vc.tabBarItem.image = [UIImage imageNamed:normalImageName];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    }
    [self addChildViewController:vc];
}

/**
 *  设置所有UITabBarItem的文字属性
 */
- (void)setupItemTitleTextAttributes
{
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateSelected];
}


@end
